﻿package com.addcn.texas.view.components
{		
	import fl.events.ListEvent;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import fl.controls.*
	import flash.text.*;
	import fl.data.DataProvider;
	import fl.controls.DataGrid;
	import fl.controls.dataGridClasses.DataGridColumn;
	import fl.managers.StyleManager;    	
	import flash.utils.getDefinitionByName;
	import com.addcn.texas.event.HallRoomListEvent;
	
	public class HallDataGrid extends MovieClip	
	{		
		private var roomList:DataGrid;
		private var roomButton:Array; 
		private var roomButtonObj:Array;		
		private var _type : int;		//當前顯示游戲場類型
		private var _data : Object;  //房間列表
		private var _hall_type : Object;  //游戲場數據
		private var _race : int; //列表是否是比賽模式
		
		function HallDataGrid () {
			_inizialize();
			
		}		
		
		private function _inizialize( ) {			
			this._hall_type = { };
			this["RefreshBtn"].addEventListener(MouseEvent.CLICK,_refreshClickHandler);
			this["JoinRoomBtn"].addEventListener(MouseEvent.CLICK, _joinClickHandler);
			this["HideFull"].addEventListener(MouseEvent.CLICK,_hideClickHandler);
			this["HideEmpty"].addEventListener(MouseEvent.CLICK,_hideClickHandler);
			
			roomList = this["DataList"];
			roomList.setStyle("textFormat", style); 
			roomList.setStyle("cellRenderer", style); 									
			roomList.setStyle("headerRenderer", CustomRender);
			roomList.resizableColumns = false;
			roomList.addEventListener(Event.CHANGE, _selectChangeHandler);
			roomList.addEventListener(ListEvent.ITEM_DOUBLE_CLICK, _joinClickHandler);			
			addChild(roomList);	
			_normalList();
		}
		
		private function _raceList() : void
		{
			if (this._race == 1) return;
			this.roomList.removeAllColumns();
			var col1:DataGridColumn = new DataGridColumn("id");
			var col2:DataGridColumn = new DataGridColumn("name");			
			var col3:DataGridColumn = new DataGridColumn("expenses");				
			var col4:DataGridColumn = new DataGridColumn("player");			
			var col5:DataGridColumn = new DataGridColumn("state");
			var col6:DataGridColumn = new DataGridColumn("reward");	
			col1.width = 54;
			col2.width = 129;
			col3.width = 132;
			col4.width = 43;
			col5.width = 50;					
			col6.width = 94;					
			
			col1.headerText = "ID";			
			col2.headerText = "房間";
			col3.headerText = "買入籌碼 + 參賽費";
			col4.headerText = "玩家";
			col5.headerText = "狀態";		
			col6.headerText = "獎勵";		
								
			roomList.addColumn(col1);
			roomList.addColumn(col2);
			roomList.addColumn(col3);
			roomList.addColumn(col4);
			roomList.addColumn(col5);
			roomList.addColumn(col6);			
			this["DatagridBg"].gotoAndStop(2);	
			this._race = 1;
		}
		
		private function _normalList() : void
		{
			if (this._race == 2) return;
			this.roomList.removeAllColumns();
			var col1:DataGridColumn = new DataGridColumn("id");
			var col2:DataGridColumn = new DataGridColumn("name");
			var col3:DataGridColumn = new DataGridColumn("player");
			var col4:DataGridColumn = new DataGridColumn("blind");
			var col5:DataGridColumn = new DataGridColumn("carry");	
			col1.width = 54;
			col2.width = 129;
			col3.width = 71;
			col4.width = 112;				
			col5.width = 137;				
			
			col1.headerText = "ID";			
			col2.headerText = "房間";
			col3.headerText = "玩家";
			col4.headerText = "小/大盲注";
			col5.headerText = "最少/最多攜帶";		
								
			roomList.addColumn(col1);
			roomList.addColumn(col2);
			roomList.addColumn(col3);
			roomList.addColumn(col4);
			roomList.addColumn(col5);							
			this["DatagridBg"].gotoAndStop(1);	
			this._race = 2;
		}
						
		private function setRoomList( ) {				
			
			var _race_tmp : int = 0;
			if (this._hall_type[this._type]) {
				_race_tmp = this._hall_type[this._type]["race"];
			}
			if (_race_tmp == 0) {
				_normalList();
			}else{
				_raceList();
			}			
		}
		
		public function setHallType(r_data : Object) : void
		{			
			var i : int = 1;					
			var _x : int = 0;
			for each(var _val in r_data)
			{					
				this._hall_type[_val["id"]] = _val;				
				try {
					var _class  = Class(getDefinitionByName("ListBtn" + _val["id"]));							
					var _obj = new _class; 
					this.addChild(_obj);
					_obj.y = 0;
					_obj.x = _x;
					_x += 66;	
					_obj.name = "ListBtn" + _val["id"];
					_obj.addEventListener(MouseEvent.CLICK, _listBtnClickHandler);
					_obj.addEventListener(MouseEvent.MOUSE_MOVE, _listBtnOverHandler);
					_obj.addEventListener(MouseEvent.MOUSE_OUT, _listBtnOutHandler);
					_obj.buttonMode = true;	
					_obj.gotoAndStop("up");							
				}catch (e) {
					trace("Hall type button class undefined!")
				}	
			}		
		}

		public function upDateList(r_data:Object) : void
		{
			if (r_data == null) return;			
			this._data  = r_data;
			var _r_type : int = r_data["type"];	
			if (_r_type != this._type && this._type !=0 ) return;			
			var _roomList : Object = r_data["roomList"];
			var _race_tmp : int = this._hall_type[_r_type]["race"];								
			var _tmp : Object;
			var _arr : Array = [];	
			if(_race_tmp == 0){
				for each(var val in _roomList)
				{
					//不顯示滿人房間
					if (this["HideFull"].selected) {
						if (val["userCount"] == 9)	continue;										
					}
					//不顯示空房間 
					if (this["HideEmpty"].selected)	{
						if ((val["userCount"] + val["sperCount"]) == 0 ) continue;						
					}				
					_tmp = {
							id: String(val["id"]),
							name: String(val["name"]),
							player: String(val["userCount"]) + " / " + String(val["sperCount"]),
							blind: String(val["SmallBlind"]) + " / " + String(val["BigBlind"]),
							carry: String(val["minChip"]) + " / " + String(val["maxChip"])							
						};						
					_arr.push(_tmp);				
				}	
			}else {
				for each( val in _roomList)
				{
					//不顯示滿人房間
					if (this["HideFull"].selected) {
						if (val["userCount"] == 9)	continue;										
					}
					//不顯示空房間 
					if (this["HideEmpty"].selected)	{
						if ((val["userCount"] + val["sperCount"]) == 0 ) continue;						
					}		
					var _game_users : int = val["userCount"] - val["sperCount"];
					var _state_str : String = val["state"] == 0 ? "準備" :"游戲中";
					_tmp = {						
							id: String(val["id"]),
							name: String(val["name"]),
							expenses: String(val["buy"]) +" + "+ String(val["cost"]),						
							player: String(_game_users) + " / " + String(val["players"]),
							state: _state_str,
							reward: "50%,30%20%"						
						};						
					_arr.push(_tmp);				
				}
			}
			
			var _obj = this.getChildByName("ListBtn" + _r_type);			
			if (_obj != null) {
				if (this._type != _r_type) {				
					_obj.gotoAndStop("up");						
				}				
			}
			this._type = _r_type;
			_obj = this.getChildByName("ListBtn" + _type);
			if(_obj != null){
				_obj.gotoAndStop("down");						
			}						
			setRoomList();
			roomList.enabled = true;
			var dp:DataProvider = new DataProvider(_arr);	
			roomList.removeAll();			
			roomList.dataProvider = dp;	
			roomList.resizableColumns = false;
		}
				
		private function _selectChangeHandler(evt : Event) : void
		{
			var _index : int = roomList.selectedIndex;
			var _col : Object = roomList.getItemAt(_index);
			var _event : HallRoomListEvent = new HallRoomListEvent(HallRoomListEvent.CHANGE_EVENT, {id:_col.id} );
			dispatchEvent(_event);			
		}
		
		private function _listBtnClickHandler(evt : Event) : void
		{
			var _str : String = evt.target.name;
			var _new_type : int = parseInt(_str.substr(7));
			if (this._type == _new_type) return;			
			var _btn : Object = this.getChildByName("ListBtn" + this._type);
			if(_btn){
				_btn.gotoAndStop("up");		
			}				
			this._type = _new_type;		
			var _event : HallRoomListEvent = new HallRoomListEvent(HallRoomListEvent.CHANGE_TYPE_EVENT, { type:this._type ,request:false} );
			dispatchEvent(_event);			
			evt.target.gotoAndStop("down");					
		}
		
		private function _listBtnOverHandler(evt: Event) : void
		{
			var _m_type : int = parseInt(evt.target.name.substr(7));
			if(_type != _m_type){
				evt.target.gotoAndStop("over");
			}
		}
		
		private function _listBtnOutHandler(evt: Event) : void
		{			
			var _m_type : int = parseInt(evt.target.name.substr(7));			
			if(_type != _m_type){
				evt.target.gotoAndStop("up");
			}
		}
		
		private function _refreshClickHandler(evt : Event) : void
		{				
			roomList.removeAll();			
			roomList.enabled = false;
			var _event : HallRoomListEvent = new HallRoomListEvent(HallRoomListEvent.REFRESH_LIST, { type: _type ,request:true} );
			dispatchEvent(_event);			
		}
		
		private function _joinClickHandler(evt : Event) : void
		{			
			var _index : int = roomList.selectedIndex;
			if (_index < 0) return;
			var _col : Object = roomList.getItemAt(_index);				
			var _rid : int = _col["id"];		
			var _event : HallRoomListEvent = new HallRoomListEvent(HallRoomListEvent.JOIN_ROOM, { rid: _rid} );
			dispatchEvent(_event);			
		}
		
		private function _hideClickHandler(evt : Event) : void
		{
			upDateList(this._data);			
		}
		
			
	}
}
